<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!--ajax 依赖于 jquery-->
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>

<script>
    /*
    * promise异步编排，结构更清晰
    * */

    //嵌套方式
    $.ajax({
        url: "mock/user.json",
        success(data) {
            console.log("查询用户：", data);
            $.ajax({
                url: `mock/user_corse_${data.id}.json`,
                success(data) {
                    console.log("查询到课程：", data);
                    $.ajax({
                        url: `mock/corse_score_${data.id}.json`,
                        success(data) {
                            console.log("查询到分数：", data);
                        },
                        error(error) {
                            console.log("出现异常了：" + error);
                        }
                    });
                },
                error(error) {
                    console.log("出现异常了：" + error);
                }
            });
        },
        error(error) {
            console.log("出现异常了：" + error);
        }
    });

    //promise
    let p = new Promise((resolve, reject) => {
        $.ajax({
            url: "mock/user.json",
            success: function (data) {
                console.log("查询用户成功:", data)
                resolve(data);//请求成功封装数据到resolve()
            },
            error: function (err) {
                reject(err);//请求失败封装数据到reject()
            }
        });
    });

    p.then((obj) => {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: `mock/user_corse_${obj.id}.json`,
                success: function (data) {
                    console.log("查询用户课程成功:", data)
                    resolve(data);
                },
                error: function (err) {
                    reject(err)
                }
            });
        })
    }).then((data) => {
        console.log("上一步的结果", data)
        $.ajax({
            url: `mock/corse_score_${data.id}.json`,
            success: function (data) {
                console.log("查询课程得分成功:", data)
            },
            error: function (err) {
            }
        });
    })


    //进一步封装，更简化
    function get(url, data) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: url,
                data: data,
                success: function (data) {
                    resolve(data);
                },
                error: function (err) {
                    reject(err)
                }
            })
        });
    };

    get("mock/user.json")
        .then((data) => {
            console.log("用户查询成功~~~:", data)
            return get(`mock/user_corse_${data.id}.json`);
        })
        .then((data) => {
            console.log("课程查询成功~~~:", data)
            return get(`mock/corse_score_${data.id}.json`);
        })
        .then((data) => {
            console.log("课程成绩查询成功~~~:", data)
        })
        .catch((err) => {
            console.log("出现异常", err)
        });

</script>
</body>

</html>